package com.njust.diary.Controlller;

import com.njust.diary.po.Note;
import com.njust.diary.po.User;
import com.njust.diary.service.NoteService;
import com.njust.diary.util.JsonUtil;
import com.njust.diary.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/report")
public class ReportServlet extends HttpServlet {
    private NoteService noteService = new NoteService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("menupage","report");
        String actionName =req.getParameter("actionName");
        if("info".equals(actionName)){
            reportInfo(req,resp);



        }else if("month".equals(actionName)){
            queryNoteCountByMonth(req,resp);

        }else if("location".equals(actionName)){
            queryNoteLocation(req,resp);

        }


    }

    private void queryNoteLocation(HttpServletRequest req, HttpServletResponse resp) {
    User user = (User) req.getSession().getAttribute("user");
    Result<List<Note>> result = noteService.queryNoteLocation(user.getUserId());
    JsonUtil.toJson(resp,result);
    }

    private void queryNoteCountByMonth(HttpServletRequest req, HttpServletResponse resp) {
    User user = (User) req.getSession().getAttribute("user");
        Result<Map<String,Object>> result = noteService.queryNoteCountByMonth(user.getUserId());
        JsonUtil.toJson(resp,result);


    }

    private void reportInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setAttribute("changePage","report/info.jsp");
    req.getRequestDispatcher("index.jsp").forward(req,resp);




    }
}
